﻿#include "ChargeType.h"
#include "ui_chargetype.h"

ChargeType::ChargeType(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChargeType)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

#if (GUN_AMOUNT == 1)
    ui->pushButton->hide();
#endif
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        countdown[i] = new CountDownTime(ui->frameMid);
        countdown[i]->setGeometry(710, 10, 30, 40);
        countdown[i]->mysetText("60");
        connect(this->countdown[i], SIGNAL(CoundDownTimeisOver()), this, SLOT(countdownOver()));
        connect(&Refreash_Thread[i], SIGNAL(Device_is_Error(int)), this, SLOT(Device_Error_or_NotConnect(int)));
        connect(&Refreash_Thread[i], SIGNAL(Device_is_NotConnect(int)), this, SLOT(Device_Error_or_NotConnect(int)));
    }
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(setCountHide(int)));
}
void ChargeType::setCountHide(int current)
{
    if(current == chargeTypePage)
    {
        ui->_focus_gun_->setText(QString::number(focus_gun_));
        if(!countdown[focus_gun_-1]->OneSecound->isActive())
            countdown[focus_gun_-1]->CountDownStart();
        for(int i=0; i<GUN_AMOUNT; i++)
        {
            if(focus_gun_-1 != i)
            {
                countdown[i]->hide();
            }
            else
            {
                countdown[i]->show();
            }
        }
    }
}
void ChargeType::Device_Error_or_NotConnect(int GUN_NO)//GUN_NO:0~7
{
    countdown[GUN_NO]->CountDownStop();
}

ChargeType::~ChargeType()
{
    delete ui;
}

void ChargeType::countdownOver()
{
    /*待完成*/
    CountDownTime* sender_ = static_cast<CountDownTime*>(sender());
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        if(sender_ == countdown[i])
        {
            Refreash_Thread[i].swipCardflag = 0;
        }
    }
}
void ChargeType::on_buttonAutoFull_clicked()
{
    /*待完成*/
    // para: {gun_idx, charge_mod[full,time,money,power], require, card_vaild, val[0, min*100, rmb*100, power*100], gun_no, vin_charge}
    onStartingCharging(focus_gun_-1, 0, 1, 1, 0, focus_gun_, GUN_STATE[focus_gun_-1].VINflag);//配置为自动充满
    emit StartCharging_RefreshChargeInfo(focus_gun_);
    GUN_STATE[focus_gun_-1].Page = chargeWatchingDCPage;
    countdown[focus_gun_-1]->CountDownStop();
}

void ChargeType::on_buttonSetTime_clicked()
{
    /*待完成*/
    GUN_STATE[focus_gun_-1].Page = settingOfChargeTimePage;
    countdown[focus_gun_-1]->CountDownStop();
}

void ChargeType::on_buttonSetMoney_clicked()
{
    /*待完成*/
    GUN_STATE[focus_gun_-1].Page = settingOfChargeMoneyPage;
    countdown[focus_gun_-1]->CountDownStop();
}

void ChargeType::on_buttonSetPower_clicked()
{
    /*待完成*/
    GUN_STATE[focus_gun_-1].Page = settingOfChargePowerPage;
    countdown[focus_gun_-1]->CountDownStop();
}

void ChargeType::on_buttonCancelCharging_clicked()
{
    /*待完成*/
//    Refreash_Thread[focus_gun_-1].swipCardflag = 0;
    GUN_STATE[focus_gun_-1].Page = returnChargeGunPage;
    countdown[focus_gun_-1]->CountDownStop();
}

void ChargeType::on_pushButton_clicked()
{
    Init_Focus();
}
